package com.shuyu.gsyvideoplayer.widget;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


import com.ruihang.ijkplaylib.R;

import java.util.ArrayList;
import java.util.List;

public class BarrageListView extends LinearLayout {

    private FadingRecyclerView recyclerView;
    private BarrageListAdapter adapter;
    private long lastPosition = 0;

    public BarrageListView(Context context) {
        super(context);
        initInflate(context);
    }

    public BarrageListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initInflate(context);
    }

    public BarrageListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initInflate(context);
    }


    protected void initInflate(Context context) {
        try {
            LayoutInflater.from(context).inflate(R.layout.barrage_list, this);
            recyclerView = (FadingRecyclerView) findViewById(R.id.barrage_list);

            recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            DisplayMetrics dm = getResources().getDisplayMetrics();
            int screenHeight = dm.heightPixels;
            int screenWidth = dm.widthPixels;
            ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams) recyclerView.getLayoutParams();
            if (screenHeight > screenWidth) {
                layoutParams.height = screenHeight * 2 / 5;
                layoutParams.width = screenWidth * 2 / 3;
            } else {
                layoutParams.width = screenWidth / 2;
                layoutParams.height = screenHeight / 2;
            }
            recyclerView.setLayoutParams(layoutParams);
            List<String> a = new ArrayList<>();
            a.add("");//初始的时候顶部留白
            a.add("");
            a.add("");
            a.add("");
            a.add("");
            a.add("");
            a.add("");
            a.add("");
            a.add("");
            a.add("");
            lastPosition = a.size() - 1;
            adapter = new BarrageListAdapter(context, a);
            recyclerView.setAdapter(adapter);
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (recyclerView.getLayoutManager() != null) {
                        getPositionAndOffset();
                    }
                }
            });
        } catch (InflateException e) {
            e.printStackTrace();

        }
    }
    //监听RecyclerView滚动状态


    /**
     * 记录RecyclerView当前位置
     */
    private void getPositionAndOffset() {
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //获取可视的第一个view
        View topView = layoutManager.getChildAt(0);
        if (topView != null) {
            //获取与该view的顶部的偏移量
//            lastOffset = topView.getTop();
            //得到该View的数组位置
            lastPosition = layoutManager.findLastVisibleItemPosition();
//            lastPosition = layoutManager.getPosition(topView);
        }
    }

    /**
     * 关闭弹幕，清空所有
     */
    public void showRecycler() {
        if (null != recyclerView) {
            recyclerView.setVisibility(View.VISIBLE);
        }
    }

    public void hideRecycler() {
        if (null != recyclerView) {
            recyclerView.setVisibility(View.GONE);
        }
    }

    public void clearAllMsg() {
        if (null != adapter) {
            adapter.clearData();
        }
    }

    public void addData(String str) {
        if (null != adapter) {
            adapter.addMessage(str);
            if (adapter.getItemCount() - lastPosition < 5) {
                lastPosition = adapter.getItemCount() - 1;
                recyclerView.scrollToPosition(adapter.getItemCount() - 1);
            }
        }
    }
}
